import * as equipmentService from '../../services/equipment';
import {queryAllRoomList} from '../../services/tRoom';
import {queryList,getRoomInfo} from '../../services/space';

export default {
  namespace: 'space',
  state: {
    current: 1,
    currentSize: 10,
    total: 0,
    presetId:'',
    list: [],
    contractList:[],
    modalVisible : false,
    currentItem:{},
    roomList:[],
    searchObject:{},
    loading:false,
    modalCurrent: 1,
    modalCurrentSize: 10,
    modalTotal: 0,
    modalList:[],
    modalLoading:false,
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
    queryPresetListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },

  },
  effects: {

    *qryById({ payload },{ call, put }){

      yield put({ type: 'updateState',  payload: { modalLoading:true,}});
      const { body } = yield call(getRoomInfo,payload);
      yield put({
          type: 'updateState',
          payload: {
            modalList: body.data,
            modalLoading:false,
          },
      });
    },

    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'updateState', payload: {roomList:body.data} });
    },

    *queryList({ payload },{ call, put }) {
      yield put({ type: 'updateState',  payload: { loading:true,}});
      const { body } = yield call(queryList,payload);
      yield put({ type: 'updateState',
      payload: { list:body.data, current:body.current, currentSize:body.size,
        total:body.iTotalRecords, datacenterId:payload.datacenterId,searchObject:payload,loading:false}});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/space/space') {
          dispatch({ type: 'queryRoom'});
          dispatch({ type: 'queryList', payload:{}});
        }
      });
    },
  },
};
